/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dusin.adserver.utils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author dusin
 */
public class TheUtil {

    //获取多店趋势数据
    public static Map<String, Object> storesTrend4Integer(String suffix, List<String> yearMonthList, List<Map<String, Object>> data) {
        Map<String, Object> resultMap = new HashMap<String,Object>();
        List<Integer> all = new ArrayList<Integer>();
        List<Integer> zj = new ArrayList<Integer>();
        List<Integer> dy = new ArrayList<Integer>();
        List<Integer> yz = new ArrayList<Integer>();
        for (String ym : yearMonthList) {
            //all.add(0);
            zj.add(0);
            dy.add(0);
            yz.add(0);
        }
        
        for (Map<String, Object> m : data) {
            String code = (String) m.get("orgCode");
            String yearMonth = (String) m.get("yearMonth");
            int index = yearMonthList.indexOf(yearMonth);
            if ("1001".equals(code)) {                
                zj.set(index,((BigDecimal) m.get("value")).intValue());                
            } else if ("1002".equals(code)) {
                dy.set(index, ((BigDecimal) m.get("value")).intValue());
            } else if ("1003".equals(code)) {
                yz.set(index,((BigDecimal) m.get("value")).intValue());
            }
        }
        for (int i = 0; i < zj.size(); i++) {
            all.add(zj.get(i) + dy.get(i) + yz.get(i));
        }
        resultMap.put("all" + suffix, all);
        resultMap.put("zj" + suffix, zj);
        resultMap.put("dy" + suffix, dy);
        resultMap.put("yz" + suffix, yz);
        return resultMap;
    }
    
    public static Map<String, Object> storesTrend4BigDecimal(String operator,String suffix, List<String> yearMonthList, List<Map<String, Object>> data) {
        Map<String, Object> resultMap = new HashMap<String,Object>();
        List<BigDecimal> all = new ArrayList<BigDecimal>();
        List<BigDecimal> zj = new ArrayList<BigDecimal>();
        List<BigDecimal> dy = new ArrayList<BigDecimal>();
        List<BigDecimal> yz = new ArrayList<BigDecimal>();
        for (String ym : yearMonthList) {           
            zj.add(BigDecimal.ZERO);
            dy.add(BigDecimal.ZERO);
            yz.add(BigDecimal.ZERO);
        }
        
        for (Map<String, Object> m : data) {
            String code = (String) m.get("orgCode");
            String yearMonth = (String) m.get("yearMonth");
            int index = yearMonthList.indexOf(yearMonth);
            BigDecimal v = ((BigDecimal) m.get("value"));
            if (v==null)
                continue;
            if ("1001".equals(code)) {                
                zj.set(index,v);                
            } else if ("1002".equals(code)) {
                dy.set(index, v);
            } else if ("1003".equals(code)) {
                yz.set(index,v);
            }
        }
        
        if("avg".equals(operator)){  //算平均
            for (int i = 0; i < zj.size(); i++) {
                BigDecimal avg = new BigDecimal(0);
                int count=0;   //不等于0的数目
                if(!zj.get(i).equals(BigDecimal.ZERO)){
                    count++;
                }
                if(!dy.get(i).equals(BigDecimal.ZERO)){
                    count++;
                }
                if(!yz.get(i).equals(BigDecimal.ZERO)){
                    count++;
                }
                if(count==0){
                    all.add(BigDecimal.ZERO);
                }else{
                    avg = avg.add(zj.get(i));
                    avg = avg.add(dy.get(i));
                    avg=avg.add(yz.get(i));
                    avg=avg.divide(new BigDecimal(count),2,BigDecimal.ROUND_HALF_UP);
                    all.add(avg);
                }
            }
        }else if("sum".equals(operator)){// 求和
            for (int i = 0; i < zj.size(); i++) {
                BigDecimal sum = new BigDecimal(0);
                sum.add(zj.get(i));
                sum.add(dy.get(i));
                sum.add(yz.get(i));
                all.add(sum);
            }
        }
        
        resultMap.put("all" + suffix, all);
        resultMap.put("zj" + suffix, zj);
        resultMap.put("dy" + suffix, dy);
        resultMap.put("yz" + suffix, yz);
        return resultMap;
    }
}
